<template>
  <div>
    <n-card :bordered="false" class="mt-4 proCard">
      <n-grid cols="2" responsive="screen">
        <n-grid-item offset="0 s:0 m:1 l:1 xl:1 2xl:1">
          <n-form
            :label-width="90"
            :model="formValue"
            :rules="rules"
            label-placement="top"
            ref="formRef"
            class="py-8"
          >
            <n-form-item label="优惠券名称" path="name">
              <n-input v-model:value="formValue.name" placeholder="优惠券名称" />
            </n-form-item>
            <n-form-item label="金额" path="amount">
              <n-input-number v-model:value="formValue.amount" placeholder="金额" />
            </n-form-item>
            <n-form-item label="生效时间" path="start_date">
              <n-date-picker type="date" v-model:value="formValue.start_date" style="width: 100%" />
            </n-form-item>
            <n-form-item label="失效时间" path="end_date">
              <n-date-picker type="date" v-model:value="formValue.end_date" style="width: 100%" />
            </n-form-item>
            <n-form-item label="类型" path="status">
              <n-radio-group v-model:value="formValue.status" name="status">
                <n-space>
                  <n-radio value="active">可领取</n-radio>
                  <n-radio value="inactive">未生效</n-radio>
                  <!-- <n-radio value="expired">已过期</n-radio> -->
                  <!-- <n-radio value="disabled">已禁用</n-radio> -->
                </n-space>
              </n-radio-group>
            </n-form-item>
            <n-form-item label="优惠券描述" path="description">
              <n-input v-model:value="formValue.description" />
            </n-form-item>
            <div style="margin-left: 80px">
              <n-space>
                <n-button type="primary" @click="formSubmit">提交</n-button>
                <n-button @click="resetForm">重置</n-button>
              </n-space>
            </div>
          </n-form>
        </n-grid-item>
      </n-grid>
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref, unref, reactive, onMounted } from 'vue';
  import { useMessage } from 'naive-ui';
  import '@vueup/vue-quill/dist/vue-quill.snow.css';
  import { ResultEnum } from '@/enums/httpEnum';
  import { useRouter } from 'vue-router';
  import { getCouponInfo, patchCoupon, putCoupon } from '@/api/coupon/coupon';
  import router from '@/router';

  const id: any = ref(useRouter().currentRoute.value.params.id);
  const rules = {
    name: {
      required: true,
      message: '请输入优惠券名称',
      trigger: 'blur',
    },
  };

  const formRef: any = ref(null);
  const message = useMessage();

  const defaultValueRef = () => ({
    id: 0,
    name: '',
    amount: 1,
    type: 'fixed',
    start_date: null,
    end_date: null,
    status: 'active',
    description: '',
  });

  let formValue = reactive(defaultValueRef());

  const getData = async () => {
    const response: any = await getCouponInfo(formValue.id);
    if (response.code === ResultEnum.SUCCESS) {
      response.data.price = parseFloat(response.data.price);
      Object.assign(formValue, response.data);
    }
  };

  function formSubmit() {
    formRef.value.validate(async (errors) => {
      if (!errors) {
        try {
          let response: any = null;
          if (formValue.id == 0) {
            response = await putCoupon(formValue);
          } else {
            response = await patchCoupon(formValue);
          }

          if (response.code === ResultEnum.SUCCESS) {
            router.push({ name: 'coupon-list' });
            message.success('操作成功');
          }
        } finally {
          // loading.value = false;
        }
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  function resetForm() {
    formRef.value.restoreValidation();
    formValue = Object.assign(unref(formValue), defaultValueRef());
  }

  onMounted(() => {
    formValue.id = id.value;
    if (formValue.id != 0) {
      getData();
    }
  });
</script>

<style lang="less"></style>
